package com.ruoyi.system.service.impl;

import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.ArrayList;
import com.ruoyi.common.core.utils.StringUtils;
import org.springframework.transaction.annotation.Transactional;
import com.ruoyi.system.domain.IaeInternalTransfer;
import com.ruoyi.system.mapper.IaeVoucherMapper;
import com.ruoyi.system.domain.IaeVoucher;
import com.ruoyi.system.service.IIaeVoucherService;

/**
 * 凭证列表Service业务层处理
 * 
 * @author ruoyi
 * @date 2025-03-01
 */
@Service
public class IaeVoucherServiceImpl implements IIaeVoucherService 
{
    @Autowired
    private IaeVoucherMapper iaeVoucherMapper;

    /**
     * 查询凭证列表
     * 
     * @param id 凭证列表主键
     * @return 凭证列表
     */
    @Override
    public IaeVoucher selectIaeVoucherById(Long id)
    {
        return iaeVoucherMapper.selectIaeVoucherById(id);
    }

    /**
     * 查询凭证列表列表
     * 
     * @param iaeVoucher 凭证列表
     * @return 凭证列表
     */
    @Override
    public List<IaeVoucher> selectIaeVoucherList(IaeVoucher iaeVoucher)
    {
        return iaeVoucherMapper.selectIaeVoucherList(iaeVoucher);
    }

    /**
     * 新增凭证列表
     * 
     * @param iaeVoucher 凭证列表
     * @return 结果
     */
    @Transactional
    @Override
    public int insertIaeVoucher(IaeVoucher iaeVoucher)
    {
        int rows = iaeVoucherMapper.insertIaeVoucher(iaeVoucher);
        insertIaeInternalTransfer(iaeVoucher);
        return rows;
    }

    /**
     * 修改凭证列表
     * 
     * @param iaeVoucher 凭证列表
     * @return 结果
     */
    @Transactional
    @Override
    public int updateIaeVoucher(IaeVoucher iaeVoucher)
    {
        iaeVoucherMapper.deleteIaeInternalTransferByVoucherDetailsId(iaeVoucher.getId());
        insertIaeInternalTransfer(iaeVoucher);
        return iaeVoucherMapper.updateIaeVoucher(iaeVoucher);
    }

    /**
     * 批量删除凭证列表
     * 
     * @param ids 需要删除的凭证列表主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteIaeVoucherByIds(Long[] ids)
    {
        iaeVoucherMapper.deleteIaeInternalTransferByVoucherDetailsIds(ids);
        return iaeVoucherMapper.deleteIaeVoucherByIds(ids);
    }

    /**
     * 删除凭证列表信息
     * 
     * @param id 凭证列表主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteIaeVoucherById(Long id)
    {
        iaeVoucherMapper.deleteIaeInternalTransferByVoucherDetailsId(id);
        return iaeVoucherMapper.deleteIaeVoucherById(id);
    }

    /**
     * 新增内部转账记录信息
     * 
     * @param iaeVoucher 凭证列表对象
     */
    public void insertIaeInternalTransfer(IaeVoucher iaeVoucher)
    {
        List<IaeInternalTransfer> iaeInternalTransferList = iaeVoucher.getIaeInternalTransferList();
        Long id = iaeVoucher.getId();
        if (StringUtils.isNotNull(iaeInternalTransferList))
        {
            List<IaeInternalTransfer> list = new ArrayList<IaeInternalTransfer>();
            for (IaeInternalTransfer iaeInternalTransfer : iaeInternalTransferList)
            {
                iaeInternalTransfer.setVoucherDetailsId(id);
                list.add(iaeInternalTransfer);
            }
            if (list.size() > 0)
            {
                iaeVoucherMapper.batchIaeInternalTransfer(list);
            }
        }
    }
}
